# ax_hip.m4: An m4 macro to detect and configure HIP
#
# Copyright © 2008 Frederic Chateau <frederic.chateau@cea.fr>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# As a special exception to the GNU General Public License, if you
# distribute this file as part of a program that contains a
# configuration script generated by Autoconf, you may include it under
# the same distribution terms that you use for the rest of that program.
#


#
# SYNOPSIS
#	AX_HIP()
#
# DESCRIPTION
#	Checks the existence of HIP binaries and libraries.
#	Options:
#	--with-hip=(path|yes|no)
#		Indicates whether to use HIP or not, and the path of a non-standard
#		installation location of HIP if necessary.
#
#	This macro calls:
#		AC_SUBST(HIP_CFLAGS)
#		AC_SUBST(HIP_LIBS)
#		AC_SUBST(HIPCC)
#		AC_SUBST(HIPCCFLAGS)
#
AC_DEFUN([AX_HIP],
[
AC_ARG_WITH([hip],
    AS_HELP_STRING([--with-hip@<:@=yes|no|DIR@:>@], [prefix where hip is installed (default=yes)]),
[
	with_hip=$withval
	if test "$withval" = "no"
	then
		want_hip="no"
	elif test "$withval" = "yes"
	then
		want_hip="yes"
	else
		want_hip="yes"
		hip_home_path=$withval
	fi
],
[
	want_hip="yes"
])

#AM_CONDITIONAL(USE_HIP, test "x${want_hip}" = xyes)

if test "$want_hip" = "yes"
then
	# check that hipcc compiler is in the path
	if test -n "$hip_home_path"
	then
	    hipcc_search_dirs="$PATH$PATH_SEPARATOR$hip_home_path/bin"
	else
	    hipcc_search_dirs=$PATH
	fi

	AC_PATH_PROG([HIPCC], [hipcc], [], [$hipcc_search_dirs])
	if test -n "$HIPCC"
	then
		have_hipcc="yes"
	else
		have_hipcc="no"
	fi

	# set HIP flags
	if test -n "$hip_home_path"
	then
	    HIP_CFLAGS="-I$hip_home_path/include"
	    HIP_LIBS="-L$hip_home_path/lib -lamdhip64"
	else
	    HIP_CFLAGS="-I/opt/rocm/hip/include/hip"
	    HIP_LIBS="-L/opt/rocm/lib -lamdhip64"
	fi

#	saved_CPPFLAGS=$CPPFLAGS
#	saved_LIBS=$LIBS
#
#	CPPFLAGS="$CPPFLAGS $HIP_CFLAGS"
#	LIBS="$LIBS $HIP_LIBS"
#
#	AC_LANG_PUSH(C)
#	AC_MSG_CHECKING([for HIP headers])
#	AC_COMPILE_IFELSE(
#	[
#		AC_LANG_PROGRAM([@%:@include <cuda.h>], [])
#	],
#	[
#		have_hip_headers="yes"
#		AC_MSG_RESULT([yes])
#	],
#	[
#		have_hip_headers="no"
#		AC_MSG_RESULT([not found])
#	])
#
#	AC_MSG_CHECKING([for HIP libraries])
#	AC_LINK_IFELSE(
#	[
#		AC_LANG_PROGRAM([@%:@include <cuda.h>],
#		[
#			CUmodule cuModule;
#			CUdeviceptr devPtr;
#			CUfunction cuFunction;
#			size_t pitch, width = 250, height = 500;
#
#			int main()
#			{
#				cuModuleLoad(&cuModule, "myModule.cubin");
#				cuMemAllocPitch(&devPtr, &pitch,width * sizeof(float), height, 4);
#				cuModuleGetFunction(&cuFunction, cuModule, "myKernel");
#				cuFuncSetBlockShape(cuFunction, 512, 1, 1);
#				cuParamSeti(cuFunction, 0, devPtr);
#				cuParamSetSize(cuFunction, sizeof(devPtr));
#				cuLaunchGrid(cuFunction, 100, 1);
#				return 0;
#			}
#		])
#	],
#	[
#		have_hip_libs="yes"
#		AC_MSG_RESULT([yes])
#	],
#	[
#		have_hip_libs="no"
#		AC_MSG_RESULT([not found])
#	])
#	AC_LANG_POP(C)
#
#	CPPFLAGS=$saved_CPPFLAGS
#	LIBS=$saved_LIBS
	
#	if test "$have_hip_headers" = "yes" -a "$have_hip_libs" = "yes" -a "$have_hipcc" = "yes"
	if test "$have_hipcc" = "yes"
	then
		have_hip="yes"
	else
		have_hip="no"
		AC_MSG_ERROR([HIP is requested but not available])
	fi
fi

AC_MSG_NOTICE([Using HIPCCFLAGS=$HIPCCFLAGS])

AC_SUBST(HIP_CFLAGS)
AC_SUBST(HIP_LIBS)
AC_SUBST(HIPCC)
AC_SUBST(HIPCCFLAGS)
])
